#ifndef VIDEORENDERTHREAD_H
#define VIDEORENDERTHREAD_H

#include <QObject>
#include <QThread>
#include <QWidget>
#include "ffinclude.h"

/*
    视频渲染线程
*/
class VideoRenderThread: public QThread
{
    Q_OBJECT
public:
    explicit VideoRenderThread(VideoState* info, QObject* parent=NULL);

    void setIS(TestSt* state);
    void setWid(WId wid);
    void setRenderRect(QRect rect);

signals:
    void error(const QString& msg);

protected:
    virtual void run() override;

private:
    bool create();
    void clean();
    void refresh(double* remaining_time);
    void render();
    void sendError(const QString& msg);

    void calculate_display_rect(SDL_Rect *rect,
                                int scr_xleft, int scr_ytop, int scr_width, int scr_height,
                                int pic_width, int pic_height, AVRational pic_sar);
    int realloc_texture(SDL_Texture **texture, Uint32 new_format, int new_width, int new_height, SDL_BlendMode blendmode, int init_texture);
    void get_sdl_pix_fmt_and_blendmode(int format, Uint32 *sdl_pix_fmt, SDL_BlendMode *sdl_blendmode);
    int upload_texture(SDL_Texture **tex, AVFrame *frame, struct SwsContext **img_convert_ctx);
    double vp_duration(VideoState *is, Frame *vp, Frame *nextvp);
    double compute_target_delay(double delay, VideoState *is);
    void update_video_pts(VideoState *is, double pts, int64_t pos, int serial);

private:
    VideoState* m_vInfo;

    WId m_wid;
    QRect m_rect;

    bool m_reqStop;
    bool m_reqPause;
    bool m_reqUpdateSize;
    bool m_reqUpdateWnd;

    SDL_Window* m_sdlWindow;
    SDL_Renderer* m_sdlRender;
    SDL_RendererInfo m_sdlRenderInfo;
    SDL_Texture* m_sdlTextture;
};

#endif // VIDEORENDERTHREAD_H




